package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczQfycbzb;
import com.punai.dataanalysis.epdatakb.mapper.JtczQfycbzbMapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.system.mapper.EcoCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JtczQfycbzbService {


    @Autowired
    private JtczQfycbzbMapper qfycbzbMapper;
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;


    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String, Object> yearMonth = dataAnalysisVo.getYearMonth();
        List<JtczQfycbzb> resultList = new ArrayList<>();
        List<JtczResultBo> wccz = qfycbzbMapper.wccz(yearMonth);
        Map<String, BigDecimal> wcczMap = wccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> clf = qfycbzbMapper.clf(yearMonth);
        Map<String, BigDecimal> clfMap = clf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> jjf = qfycbzbMapper.jjf(yearMonth);
        Map<String, BigDecimal> jjfMap = jjf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> jxf = qfycbzbMapper.jxf(yearMonth);
        Map<String, BigDecimal> jxfMap = jxf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> qtfy = qfycbzbMapper.qtfy(yearMonth);
        Map<String, BigDecimal> qtfyMap = qtfy.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> rgf = qfycbzbMapper.rgf(yearMonth);
        Map<String, BigDecimal> rgfMap = rgf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> qtzjf = qfycbzbMapper.qtzjf(yearMonth);
        Map<String, BigDecimal> qtzjfMap = qtzjf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> qzglgz = qfycbzbMapper.qzglgz(yearMonth);
        Map<String, BigDecimal> qzglgzMap = qzglgz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> qzlsssf = qfycbzbMapper.qzlsssf(yearMonth);
        Map<String, BigDecimal> qzlsssfMap = qzlsssf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<EcoCompany> companyList = ecoCompanyMapper.selectList(
                Wrappers.<EcoCompany>lambdaQuery().eq(EcoCompany::getStatus,"0")
        );

        if (CollUtil.isEmpty(companyList)) {
            throw new ServiceException("无统计单位!");
        }

        List<EcoCompany> tjdw = companyList.stream().filter(e -> StrUtil.equals("T", e.getType())).collect(Collectors.toList());
        List<EcoCompany> zydw = companyList.stream().filter(e -> StrUtil.equals("Z", e.getType())).collect(Collectors.toList());

        List<String> tjdwcompanyNames = tjdw.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());
        List<String> zydwcompanyNames = zydw.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());

        List<JtczQfycbzb> tjList = new ArrayList<>();
        for (String companyName : tjdwcompanyNames) {
            JtczQfycbzb qfycbzb = new JtczQfycbzb();
            qfycbzb.setDeptName(companyName);
            qfycbzb.setBnwccz(wcczMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setRgf(rgfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setClf(clfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setJxf(jxfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQtzjf(qtzjfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQzlsssf(qzlsssfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setJjf(jjfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQzglgz(qzglgzMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQtfy(qtfyMap.getOrDefault(companyName, BigDecimal.ZERO));
            calcZbColumn(qfycbzb);
            tjList.add(qfycbzb);
        }
        calcPm(tjList);

        JtczQfycbzb tjdwxj = new JtczQfycbzb();
        tjdwxj.setDeptName("土建单位小计");
        calcXjHj(tjList,tjdwxj);
        tjList.add(tjdwxj);
        resultList.addAll(tjList);


        List<JtczQfycbzb> zyList = new ArrayList<>();
        for (String companyName : zydwcompanyNames) {
            JtczQfycbzb qfycbzb = new JtczQfycbzb();
            qfycbzb.setDeptName(companyName);
            qfycbzb.setBnwccz(wcczMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setRgf(rgfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setClf(clfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setJxf(jxfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQtzjf(qtzjfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQzlsssf(qzlsssfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setJjf(jjfMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQzglgz(qzglgzMap.getOrDefault(companyName, BigDecimal.ZERO));
            qfycbzb.setQtfy(qtfyMap.getOrDefault(companyName, BigDecimal.ZERO));
            calcZbColumn(qfycbzb);
            zyList.add(qfycbzb);
        }
        calcPm(zyList);
        JtczQfycbzb zydwxj = new JtczQfycbzb();
        zydwxj.setDeptName("专业单位小计");
        calcXjHj(zyList,zydwxj);
        zyList.add(zydwxj);


        resultList.addAll(zyList);

        List<JtczQfycbzb> xjList = new ArrayList<>();
        xjList.add(tjdwxj);
        xjList.add(zydwxj);
        JtczQfycbzb hj= new JtczQfycbzb();
        hj.setDeptName("合计");
        calcXjHj(xjList,hj);
        resultList.add(hj);
        return resultList;
    }

    /**
     * @param resultList
     * @param xj
     */
    private void calcXjHj(List<JtczQfycbzb> resultList, JtczQfycbzb xj) {
        BigDecimal wccz     =  BigDecimal.ZERO;
        BigDecimal rgf     =  BigDecimal.ZERO;
        BigDecimal clf     =  BigDecimal.ZERO;
        BigDecimal jxf     =  BigDecimal.ZERO;
        BigDecimal qtzjf   =  BigDecimal.ZERO;
        BigDecimal qzlsssf =  BigDecimal.ZERO;
        BigDecimal jjf     =  BigDecimal.ZERO;
        BigDecimal qzglgz  =  BigDecimal.ZERO;
        BigDecimal qtfy    =  BigDecimal.ZERO;

        for (JtczQfycbzb qfycbzb : resultList) {
            wccz    =  NumberUtil.add(wccz   ,qfycbzb.getBnwccz());
            rgf     =  NumberUtil.add(rgf    ,qfycbzb.getRgf());
            clf     =  NumberUtil.add(clf    ,qfycbzb.getClf());
            jxf     =  NumberUtil.add(jxf    ,qfycbzb.getJxf());
            qtzjf   =  NumberUtil.add(qtzjf  ,qfycbzb.getQtzjf());
            qzlsssf =  NumberUtil.add(qzlsssf,qfycbzb.getQzlsssf());
            jjf     =  NumberUtil.add(jjf    ,qfycbzb.getJjf());
            qzglgz  =  NumberUtil.add(qzglgz ,qfycbzb.getQzglgz());
            qtfy    =  NumberUtil.add(qtfy   ,qfycbzb.getQtfy());
        }

        xj.setBnwccz(  wccz    );
        xj.setRgf(     rgf     );
        xj.setClf(     clf     );
        xj.setJxf(     jxf     );
        xj.setQtzjf(   qtzjf   );
        xj.setQzlsssf( qzlsssf );
        xj.setJjf(     jjf     );
        xj.setQzglgz(  qzglgz  );
        xj.setQtfy(    qtfy    );
        calcZbColumn(xj);
    }

    private void calcZbColumn(JtczQfycbzb qfycbzb) {
        // 计算占比
        BigDecimal bnwccz = qfycbzb.getBnwccz();

        BigDecimal rgf      = qfycbzb.getRgf();
        BigDecimal clf      = qfycbzb.getClf();
        BigDecimal jxf      = qfycbzb.getJxf();
        BigDecimal qtzjf    = qfycbzb.getQtzjf();
        BigDecimal qzlsssf  = qfycbzb.getQzlsssf();
        BigDecimal jjf      = qfycbzb.getJjf();
        BigDecimal qzglgz   = qfycbzb.getQzglgz();
        BigDecimal qtfy     = qfycbzb.getQtfy();

        qfycbzb.setRgfzb(NumberUtil.divideUseProportion(rgf,2,bnwccz));
        qfycbzb.setClfzb(NumberUtil.divideUseProportion(clf,2,bnwccz));
        qfycbzb.setJxfzb(NumberUtil.divideUseProportion(jxf,2,bnwccz));
        qfycbzb.setQtzjfzb(NumberUtil.divideUseProportion(qtzjf,2,bnwccz));
        qfycbzb.setQzlsssfzb(NumberUtil.divideUseProportion(qzlsssf,2,bnwccz));
        qfycbzb.setJjfzb(NumberUtil.divideUseProportion(jjf,2,bnwccz));
        qfycbzb.setQzglgzzb(NumberUtil.divideUseProportion(qzglgz,2,bnwccz));
        BigDecimal qfxcbje = NumberUtil.adds(rgf, clf, jxf, qtzjf, qzlsssf, jjf, qzglgz, qtfy);
        qfycbzb.setQfycbje(qfxcbje);
        qfycbzb.setLrl(NumberUtil.divideUseProportion(NumberUtil.subtract(bnwccz,qfxcbje),2,bnwccz));
    }

    private void calcPm(List<JtczQfycbzb> resultList) {
        resultList.sort(Comparator.comparing(JtczQfycbzb::getRgfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setRgfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getClfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setClfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getJxfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setJxfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getQtzjfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setQtzjfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getQzlsssfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setQzlsssfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getJjfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setJjfpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getQzglgzzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setQzglgzpm(i+1);
        }
        resultList.sort(Comparator.comparing(JtczQfycbzb::getLrl,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczQfycbzb qfycbzb = resultList.get(i);
            qfycbzb.setPm(i+1);
        }
    }

}
